package com.dmap.analysispoint.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dmap.analysispoint.constants.BurialPointConstant;
import com.dmap.analysispoint.service.BurialPointInfoAnalysisByDateService;
import com.dmap.base.send.email.MailService;
import com.dmap.base.units.DateUtil;
import com.dmap.schedule.annotation.ElasticJobConf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import javax.mail.MessagingException;

/**
 * @author guanming
 * @date 18-7-13下午4:37
 * @Description: 按小时对埋点信息定时自动分析处理
 */
@ElasticJobConf(name="按小时对埋点信息定时自动分析处理", cron="0 20 * * * ?", eventTraceRdbDataSource="datasourcelog")
public class BurialPointInfoAnalysisByHourJob implements SimpleJob {

    private final static Logger logger = LoggerFactory.getLogger(BurialPointInfoAnalysisByHourJob.class);

    @Value("${spring.profiles.active}")
    private String environment;

    @Resource
    private BurialPointInfoAnalysisByDateService burialPointInfoAnalysisByDateService;

    @Override
    public void execute(ShardingContext shardingContext) {
        long startTime = DateUtil.getHourOfMillis() - BurialPointConstant.ONE_DAY_TIME_MILLISECONDS / 24;
        long endTime = DateUtil.getHourOfMillis() - 1;
        try {
            logger.info("-----------按小时对埋点信息定时自动分析处理(新增用户)开始-----------------");
            burialPointInfoAnalysisByDateService.insertInfoToBurialPointAddUser(startTime, endTime);
            MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(新增用户)定时任务成功");
        } catch (MessagingException me) {
            logger.error("按按小时对埋点信息定时自动分析处理(新增用户)定时任务 MailService.sendMail error:  {}", me);
        } catch (Exception e) {
            logger.error("按小时对埋点信息定时自动分析处理(新增用户)  error: " + e);
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(新增用户)定时任务 error: " + e);
            } catch (MessagingException me) {
                logger.error("按按小时对埋点信息定时自动分析处理(新增用户)定时任务 MailService.sendMail error:  {}", me);
            }
        }

        try {
            logger.info("-----------按小时对埋点信息定时自动分析处理(新增手机)开始-----------------");
            burialPointInfoAnalysisByDateService.insertInfoToBurialPointAddPhone(startTime, endTime);
            MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(新增手机)定时任务成功");
        } catch (MessagingException me) {
            logger.error("按小时对埋点信息定时自动分析处理(新增手机)定时任务 MailService.sendMail error:  {}", me);
        } catch (Exception e) {
            logger.error("按小时对埋点信息定时自动分析处理(新增手机)  error: " + e);
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(新增手机)定时任务 error: " + e);
            } catch (MessagingException me) {
                logger.error("按小时对埋点信息定时自动分析处理(新增手机)定时任务 MailService.sendMail error:  {}", me);
            }
        }

        try {
            logger.info("-----------按小时对埋点信息定时自动分析处理(新增设备)开始-----------------");
            burialPointInfoAnalysisByDateService.insertInfoToBurialPointAddEquipment(startTime, endTime);
            MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(新增设备)定时任务成功");
        } catch (MessagingException me) {
            logger.error("按小时对埋点信息定时自动分析处理(新增设备)定时任务 MailService.sendMail error:  {}", me);
        } catch (Exception e) {
            logger.error("按小时对埋点信息定时自动分析处理(新增设备)  error: " + e);
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(新增设备)定时任务 error: " + e);
            } catch (MessagingException me) {
                logger.error("按小时对埋点信息定时自动分析处理(新增设备)定时任务 MailService.sendMail error:  {}", me);
            }
        }

        try {
            logger.info("-----------按小时对埋点信息定时自动分析处理(按用户去重)开始-----------------");
            burialPointInfoAnalysisByDateService.insertInfoToBurialPointHourUser(startTime, endTime);
            MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(按用户去重)定时任务成功");
        } catch (MessagingException me) {
            logger.error("按小时对埋点信息定时自动分析处理(按用户去重)定时任务 MailService.sendMail error:  {}", me);
        } catch (Exception e) {
            logger.error("按小时对埋点信息定时自动分析处理(按用户去重)  error: " + e);
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(按用户去重)定时任务 error: " + e);
            } catch (MessagingException me) {
                logger.error("按小时对埋点信息定时自动分析处理(按用户去重)定时任务 MailService.sendMail error:  {}", me);
            }
        }

        try {
            logger.info("-----------按小时对埋点信息定时自动分析处理(按手机唯一标识imei去重)开始-----------------");
            burialPointInfoAnalysisByDateService.insertInfoToBurialPointHourPhone(startTime, endTime);
            MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(按手机唯一标识imei去重)定时任务成功");
        } catch (MessagingException me) {
            logger.error("按小时对埋点信息定时自动分析处理(按手机唯一标识imei去重)定时任务 MailService.sendMail error:  {}", me);
        } catch (Exception e) {
            logger.error("按小时对埋点信息定时自动分析处理(按手机唯一标识imei去重)  error: " + e);
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(按手机唯一标识imei去重)定时任务 error: " + e);
            } catch (MessagingException me) {
                logger.error("按小时对埋点信息定时自动分析处理(按手机唯一标识imei去重)定时任务 MailService.sendMail error:  {}", me);
            }
        }

        try {
            logger.info("-----------按小时对埋点信息定时自动分析处理(按设备sn去重)开始-----------------");
            burialPointInfoAnalysisByDateService.insertInfoToBurialPointHourEquipment(startTime, endTime);
            MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(按设备sn去重)定时任务成功");
        } catch (MessagingException me) {
            logger.error("按小时对埋点信息定时自动分析处理(按设备sn去重)定时任务 MailService.sendMail error:  {}", me);
        } catch (Exception e) {
            logger.error("按小时对埋点信息定时自动分析处理(按设备sn去重)  error: " + e);
            try {
                MailService.sendMail(BurialPointConstant.EMAIL_ADDRESS, BurialPointConstant.EMAIL_SUBJECT + " — " + "环境:" + environment, "按小时对埋点信息定时自动分析处理(按设备sn去重)定时任务 error: " + e);
            } catch (MessagingException me) {
                logger.error("按小时对埋点信息定时自动分析处理(按设备sn去重)定时任务 MailService.sendMail error:  {}", me);
            }
        }
    }
}
